<?php
/**
 * ClassName: medicine
 * Description:
 * @Author 周昌
 * @Create 2023/11/18 10:09
 */

namespace app;


use epii\server\Args;
use libs\Tool;
use model\MedicineModel;
use model\MemberModel;
use service\admin\HospitalService;
use service\admin\TreatmentCateService;
use service\admin\MedicineService;
use think\Db;

class medicine extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }
        //表达式查询 库存小于等于预警库存的数据
        $alert_inventory=Args::params('alert_inventory');
        if($alert_inventory){
            $map[]=['inventory','exp',Db::raw('<=alert_inventory')];
        }
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        $abbr_name = Args::params('abbr_name');
        if ($abbr_name) {
            $map[] = ['abbr_name', "LIKE", '%' . $abbr_name . '%'];
        }

        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        echo MedicineService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //登录权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if(!empty($hospital_id)){
                $data['hospital_id']=$hospital_id;
            }
            //验证
            $this->validate($data,[
                'name|药品名称'=>'require',
                'abbr_name|药品简称'=>'require',
                'specs|药品规格'=>'require',
                'common_does|常用剂量'=>'require|integer|>=:0',
                'price|单价'=>'require|float|>:0|>:purchase_price',
                'unit|单位'=>'require',
                'purchase_price|进价'=>'require|float|>:0',
                'inventory|库存'=>'require|integer|>:0',
                'alert_inventory|预警库存'=>'require|integer|>:0',
                'hospital_id|所属医院'=>'require|integer|>=:0',
                'status|状态'=>'require|integer|>=:0',
                'sort|排序'=>'integer|>=:0',
            ]);
            if ($id) {
                $res = MedicineService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $res = MedicineService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? MedicineModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_status = MedicineService::statusSelect();
        $this->_as_hospitals = HospitalService::getSelect();
        $this->adminUiDisplay();
    }
    //修改状态
    public function status(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status');
        $res=MedicineService::status($id,$status);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断治疗项目下是否已经有订单，无则可以删除
        $res = MedicineService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    //入库
    public function enter()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //验证
            $this->validate($data,[
                'quantity|入库数量'=>'require|integer|>:0',
            ]);
            $res=MedicineService::updateInventory(['id'=>$id],$data['quantity']);
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? MedicineModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->adminUiDisplay();
    }
    //库存预警
    public function warning()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->_as_alert_inventory=1;
        $this->adminUiDisplay();
    }
}